<?php

namespace app\shop\controller;

use think\Controller;

class Business extends Controller
{
    public function _initialize()
    {
        // 继承父类
        parent::_initialize();
        $this->BusinessModel = model('Business.Business');
        $this->SourceModel = model('Business.Source');
    }

    // 登陆
    public function login()
    {
        if ($this->request->isPost())
        {
            $mobile = $this->request->param('mobile', '', 'trim');
            $password = $this->request->param('password', '', 'trim');

            // 数据查询用户信息是否存在
            $Business = $this->BusinessModel->where('mobile', $mobile)->find();

            // 登陆
            if ($Business)
            {
                $password = md5($password . $Business['salt']);

                if ($password != $Business['password'])
                {
                    $this->error('密码错误');
                    exit;
                }

                // 删除不需要的信息
                unset($Business['password']);
                unset($Business['salt']);

                $this->success('登陆成功', '', $Business);
            }
            // 注册
            else
            {
                // 密码盐
                $salt = build_randstr();

                // 密码加密
                $password = md5($password . $salt);

                // 查询某个字段的值
                $sourceid = $this->SourceModel->where('name', 'like', '%云课堂%')->value('id');

                $data = [
                    'mobile' => $mobile,
                    'nickname' => $mobile,
                    'password' => $password,
                    'salt' => $salt,
                    'gender' => '0',
                    'sourceid' => $sourceid ?? null,
                    'auth' => '0'
                ];


                $result = $this->BusinessModel->validate('common/Business/Business')->save($data);

                if ($result === false)
                {
                    $this->error($this->BusinessModel->getError());
                    exit;
                }
                else
                {
                    // 通过自增ID获取刚注册的客户信息
                    $Business = $this->BusinessModel->find($this->BusinessModel->id);

                    // 删除不需要的信息
                    unset($Business['password']);
                    unset($Business['salt']);

                    $this->success('注册登录成功', '', $Business);
                    exit;
                }
            }
        }
    }

    // 校验
    public function check()
    {
        if ($this->request->isPost())
        {
            $busid = $this->request->param('busid', 0, 'trim');
            $mobile = $this->request->param('mobile', '', 'trim');

            $Business = $this->BusinessModel->where(['id' => $busid, 'mobile' => $mobile])->find();

            if (!$Business)
            {
                $this->error('用户不存在');
                exit;
            }

            // 删除不需要的信息
            unset($Business['password']);
            unset($Business['salt']);

            $this->success('登陆成功', '', $Business);
        }
    }
}
